<template>
    <div>
        <el-form :model="registerForm" status-icon :rules="rules" ref="registerForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
                <el-input type="text" v-model="registerForm.username" autocomplete="off" clearable></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phoneNumber">
                <el-input type="text" v-model="registerForm.phoneNumber" autocomplete="off" clearable></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="registerForm.pass" autocomplete="off" clearable></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="registerForm.checkPass" autocomplete="off" clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('registerForm')">注册</el-button>
                <el-button @click="resetForm('registerForm')">重置</el-button>
            </el-form-item>
        </el-form>         
    </div>
   
</template>

<script>
import { REGEXP } from '@/util/regexp'
  export default {
    name:'Register',
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.registerForm.checkPass !== '') {
            this.$refs.registerForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.registerForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      var validatePhoneNumber = (rule, value, callback) => {
          if(!value){
              callback(new Error('请输入手机号码！'));
          }else{
              if(!REGEXP['phoneNumber'].test(value)){
                  callback(new Error('请输入正确的手机号码！'));
              }else{
                  callback();
              }
          }
      }
      return {
        registerForm: {
          pass: '',
          checkPass: '',
          phoneNumber: '',
          username: ''
        },
        rules: {
          pass: [
            { required: true, validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { required: true, validator: validatePass2, trigger: 'blur' }
          ],
          phoneNumber: [
              { required: true, validator: validatePhoneNumber, trigger: 'blur' }
          ],
          username: [
              { required: true, message: '用户名不能为空！', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style lang="scss" scoped>
    .el-form {
        width: 60%;
        margin: 100px auto;
    }      

</style>